<template>
  <div class="modal">
    <el-dialog v-model="dialogVisible" :title="modalConfig.header.newTitle" width="30%" center>
      <div class="form">
        <el-form :model="formData" label-width="80px" size="large">
          <template v-for="item in modalConfig.formItems" :key="item.prop">
            <el-form-item :label="item.label" :prop="item.prop">
              <template v-if="item.type === 'input'">
                <el-input v-model="formData[item.prop]" :placeholder="item.placeholder" />
              </template>
              <template v-if="item.type === 'password'">
                <el-input
                  show-password
                  v-model="formData[item.prop]"
                  :placeholder="item.placeholder"
                />
              </template>
              <template v-if="item.type === 'select'">
                <el-select
                  v-model="formData.parentId"
                  :placeholder="item.placeholder"
                  style="width: 100%"
                >
                  <template v-for="value in item.options" :key="value.value">
                    <el-option :value="value.value" :label="value.label" />
                  </template>
                </el-select>
              </template>
              <template v-if="item.type === 'date-picker'">
                <el-date-picker
                  type="daterange"
                  range-separator="-"
                  start-placeholder="开始时间"
                  end-placeholder="结束时间"
                  v-model="formData[item.prop]"
                />
              </template>
              <template v-if="item.type === 'custom'">
                <slot :name="item.slotName"></slot>
              </template>
            </el-form-item>
          </template>
        </el-form>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleConfirmClick">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts" name="modal">
import { storeToRefs } from "pinia";
import { reactive, ref } from "vue";
import useSystemStore from "@/stores/modules/main/system/system";

const dialogVisible = ref(false);
const isEdit = ref(false);
const editData = ref();

// 定义数据绑定
const formData = reactive<any>({
  name: "",
  leader: "",
  parentId: ""
});

const { modalConfig, treeInfo } = defineProps(["modalConfig", "treeInfo"]);
// 点击确定
const systemStore = useSystemStore();
const { departmentList } = storeToRefs(systemStore);

const initialData: any = {};
for (const item of modalConfig?.formItems) {
  initialData[item.prop] = item.initialValue ?? "";
}

function handleConfirmClick() {
  dialogVisible.value = false;
  // 判断是否存在含树形菜单权限的formData
  let treeFormData = { ...formData };
  if (treeInfo) {
    treeFormData = {
      ...treeFormData,
      ...treeInfo
    };
  }
  console.log(treeFormData);
  if (!isEdit.value) {
    systemStore.newPageDataAction(modalConfig.pageName, treeFormData);
  } else {
    systemStore.editPageDataAction(modalConfig.pageName, editData.value.id, treeFormData);
  }
}

// 新建或者编辑
function setDialogVisible(isNew: boolean = true, data: any = {}) {
  dialogVisible.value = true;
  isEdit.value = !isNew;
  editData.value = data;
  for (const key in formData) {
    if (isNew) {
      formData[key] = "";
    } else {
      formData[key] = data[key];
    }
  }
}

defineExpose({
  setDialogVisible
});
</script>

<style scoped lang="less">
.form {
  padding: 10px 30px;
}
</style>
